package py.edu.ucom.c417.cadira.tablero;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;

public class TableroView extends View {

	private Paint PintarTablero;
	private Tablero tablero;
	private RectF rectTouch;
	
	public Tablero getTablero() {
		return tablero;
		
	}

	public void setTablero(Tablero tablero) {
		this.tablero = tablero;
	}

	private int tamCasilla;

	Context context;

	public TableroView(Context context) {
		super(context);
		this.rectTouch = new RectF();
		this.context = context;
		setBackgroundColor(Color.BLACK);

		PintarTablero = new Paint();
		if (getWidth() < getHeight())
			tamCasilla = getWidth() / 11;
		else
			tamCasilla = getHeight() / 11;

	}

	public RectF getRectTouch() {
		return rectTouch;
	}

	public void setRectTouch(RectF rectTouch) {
		this.rectTouch = rectTouch;
	}

	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
		if (getWidth() < getHeight())
			tamCasilla = getWidth() / 10;
		else
			tamCasilla = getHeight() / 10;
		if (tablero == null)
			tablero = new Tablero(tamCasilla,this.context);
		PintarTablero.setColor(Color.BLUE);

		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				PintarTablero.setColor(Color.GRAY);
				if (tablero.getTablero()[i][j].isEsAgua() && tablero.getTablero()[i][j].getFicha() == null)
					PintarTablero.setColor(Color.BLUE);
				canvas.drawRect(tablero.getTablero()[i][j].getRecCasilla(), PintarTablero);
				if(tablero.getTablero()[i][j].getFicha() != null){
					Rect r = new Rect();
					r = rectfToRect(tablero.getTablero()[i][j].getRecCasilla());
					tablero.getTablero()[i][j].getFicha().getImagen().setBounds(r);
					tablero.getTablero()[i][j].getFicha().getImagen().draw(canvas);
					PintarTablero.setColor(Color.WHITE);
					canvas.drawRect(this.rectTouch, PintarTablero);
					PintarTablero.setColor(Color.BLUE);
				}
				
			}
		}

		invalidate();
	}
	
	private Rect rectfToRect(RectF rf){
		Rect r = new Rect();
		r.left = (int) rf.left;
		r.top = (int) rf.top;
		r.right = (int) rf.right;
		r.bottom = (int) rf.bottom;
		return r;
	}
}
